<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
          <div class="title">当前城市</div>
          <div class="button-list">
              <div class="button-wrapper">
                <div class="buttom">{{this.$store.state.city}}</div>
              </div>
          </div>
      </div>
      <div class="area">
          <div class="title">热门城市</div>
          <div class="button-list">
              <div class="button-wrapper"  v-for="item in msg.hotCity" :key="item.id">
                <div class="buttom" @click='handleCity(item.name)'>{{item.name}}</div>
              </div>
          </div>
      </div>
      <div class="area" v-for="(item, key) in msg.cities" :key="key">
            <div class="title" :ref="key">{{key}}</div>
            <div class="city-names">
              <div class="city-name border-topbottom"  v-for="info in item" :key="info.id" @click='handleCity(info.name)'>{{info.name}}</div>
            </div>
      </div>
    </div>
  </div>
</template>
<script>
import Bscroll from 'better-scroll'
export default {
  name: 'cityList',
  props: {
    msg: Object,
    elementText: String
  },
  methods: {
    handleCity (city) {
      this.$store.dispatch('changeCity', city)
      this.$router.push('/')
    }
  },
  watch: {
    elementText () {
      if (this.elementText) {
        console.log(this.$refs)
        const element = this.$refs[this.elementText][0]
        this.scroll.scrollToElement(element)
      }
    }
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper, {click: true})
  },
  updated () {
    this.scroll && this.scroll.refresh()
  },
  activated () {
    this.scroll && this.scroll.refresh()
  }

}
</script>
<style lang="stylus" scoped>
.list
  overflow hidden
  position absolute
  left: 0
  right: 0
  top: 1.72rem
  bottom: 0
  .area
    width: 100%
    .title
      font-size: .26rem
      line-height: .54rem
      color: #666
      background: #eee
      text-indent: .2rem
    .button-list
      padding: .1rem .6rem .1rem .1rem
      overflow hidden
      .button-wrapper
        width: 33.33%
        float: left
        .buttom
          padding: .1rem
          margin: .1rem
          line-height: .35rem
          border: .02rem solid #ccc
          text-align: center
          border-radius: .1rem
    .city-names
      .city-name
        line-height: .6rem
        text-indent: .2rem
</style>
